﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.WinService;
using System.Linq;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using DevExpress.XtraEditors.Controls;
using ECRMS.WinForm.BaseClass.Other;

namespace ECRMS.WinForm.ModuleEdits
{
    public partial class PostEdit : BaseEdit
    {
        public PostEdit(Rectangle r, OperationEnum op)
        {
            InitializeComponent();
            InitView(r, op);
            InitData();
        }

        private void InitData()
        {
            switch (base.Op)
            {
                case OperationEnum.New:
                    txtTitle.Text = "";
                    recContent.WordMLText = "";
                    break;
                case OperationEnum.Modify:
                    txtTitle.Text = core.CorePost.OperatorRow.Title;
                    recContent.WordMLText = System.Text.Encoding.UTF8.GetString(core.CorePost.OperatorRow.Content.ToArray());
                    break;
                default:
                    break;
            }
        }

        private void InitView(Rectangle r, OperationEnum op)
        {
            this.Text = "新闻编辑";
            this.Left = r.Left + (r.Width - this.Width) / 2;
            this.Top = r.Top + (r.Height - this.Height) / 2;

            base.NewBarButtonVisiable = false;
            base.SaveBarButtonVisiable = true;
            base.EditBarButtonVisiable = false;
            base.DelBarButtonVisiable = false;
            base.RefreshBarButtonVisiable = false;
            base.ResetBarButtonVisiable = true;
            base.OtherBarButtonVisiable = false;
            base.Other1ButtonVisiable = false;
            base.Op = op;
        }
        //保存
        public override void saveBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.saveBarButtonClick(sender, e);
            core.CorePost.OperatorRow.Title = txtTitle.Text.Trim();

            core.CorePost.OperatorRow.Content = System.Text.Encoding.UTF8.GetBytes(recContent.WordMLText);

            FuncResult result = core.CorePost.UpdatePost();

            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }
        //重置
        public override void resetBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.resetBarButtonClick(sender, e);
            InitData();
        }
    }
}